#include "view_metronome.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "Motor.h"
#include "app_timer.h"
#include "view_common.h"
#include "app_trace.h"
#include "language_text.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
const unsigned char bmp_6009_unit_bpm[] = {
    /* 数组大小：280 颜色数：16 图片：6009_unit_bpm.bmp*/
    0x1f, 0x00, 0x0e, 0x00, 0x10, 0x00, 0x5d, 0xef, 0x8a, 0x52, 0x00, 0x00, 0x9e, 0xf7, 0x0c, 0x63, 0x8e, 0x73, 0x55, 0xad, 0xd7, 0xbd, 0x9a, 0xd6, 0x92, 0x94, 0x14, 0xa5, 0x04, 0x21, 0xdb, 0xde,
    0x10, 0x84, 0x18, 0xc6, 0xc7, 0x39, 0x00, 0x01, 0x02, 0x9c, 0x03, 0x04, 0x02, 0x9c, 0x03, 0x04, 0x02, 0x9c, 0x03, 0x05, 0x06, 0x03, 0x81, 0x07, 0x01, 0x02, 0x81, 0x01, 0x00, 0x01, 0x07, 0x03,
    0x08, 0x05, 0x02, 0x81, 0x01, 0x00, 0x09, 0x00, 0x81, 0x0a, 0x02, 0x05, 0x08, 0x03, 0x08, 0x05, 0x02, 0x03, 0x00, 0x06, 0x0b, 0x81, 0x06, 0x00, 0x01, 0x02, 0x04, 0x03, 0x0c, 0x09, 0x02, 0x05,
    0x00, 0x0d, 0x02, 0x04, 0x03, 0x08, 0x01, 0x81, 0x08, 0x0e, 0x00, 0x05, 0x02, 0x05, 0x00, 0x05, 0x03, 0x06, 0x02, 0x83, 0x06, 0x07, 0x02, 0x04, 0x03, 0x0d, 0x02, 0x82, 0x05, 0x00, 0x02, 0x04,
    0x03, 0x0f, 0x02, 0x81, 0x0f, 0x03, 0x0d, 0x02, 0x82, 0x0d, 0x0c, 0x03, 0x04, 0x02, 0x83, 0x04, 0x03, 0x02, 0x04, 0x03, 0x02, 0x84, 0x03, 0x04, 0x81, 0x03, 0x02, 0x83, 0x03, 0x04, 0x02, 0x82,
    0x04, 0x03, 0x81, 0x04, 0x02, 0x83, 0x04, 0x03, 0x02, 0x04, 0x03, 0x02, 0x84, 0x03, 0x04, 0x81, 0x03, 0x02, 0x83, 0x03, 0x04, 0x02, 0x82, 0x04, 0x03, 0x81, 0x06, 0x02, 0x83, 0x06, 0x07, 0x02,
    0x04, 0x03, 0x0d, 0x02, 0x82, 0x05, 0x00, 0x02, 0x04, 0x03, 0x02, 0x83, 0x03, 0x04, 0x02, 0x82, 0x04, 0x03, 0x81, 0x00, 0x06, 0x0b, 0x81, 0x0a, 0x00, 0x01, 0x02, 0x04, 0x03, 0x00, 0x05, 0x02,
    0x05, 0x00, 0x0d, 0x02, 0x04, 0x03, 0x02, 0x83, 0x03, 0x04, 0x02, 0x82, 0x04, 0x03, 0x00, 0x04, 0x06, 0x03, 0x81, 0x07, 0x01, 0x02, 0x81, 0x04, 0x03, 0x01, 0x0c, 0x03, 0x0c, 0x05, 0x02, 0x81,
    0x01, 0x00, 0x02, 0x83, 0x00, 0x01, 0x02, 0x82, 0x01, 0x00, 0x02, 0x88, 0x04, 0x03, 0x02, 0x9c, 0x04, 0x03, 0x02, 0x9c, 0x01, 0x00, 0x02, 0x93,
};
const unsigned char bmp_6110_reset[] = {
    /* 数组大小：225 颜色数：41 图片：6110-rest-198-51.bmp*/
    0x12, 0x00, 0x10, 0x00, 0x29, 0x00, 0x00, 0x00, 0xe0, 0x20, 0xa0, 0x8b, 0xa0, 0xdd, 0x80, 0xfe, 0xc0, 0xe5, 0xe0, 0x9b, 0x00, 0x29, 0x20, 0x7b, 0x40, 0xf6, 0xe0, 0xc4, 0xc0,
    0xbc, 0x00, 0xee, 0x20, 0xa4, 0x00, 0xc5, 0x60, 0x39, 0xc0, 0x18, 0x00, 0x7b, 0xe0, 0x93, 0x80, 0x62, 0x00, 0x9c, 0x60, 0x10, 0x60, 0xd5, 0x80, 0x39, 0x40, 0x83, 0x20, 0x29,
    0x20, 0xf6, 0x60, 0xfe, 0x60, 0xac, 0x20, 0xcd, 0x40, 0xa4, 0x20, 0x31, 0x40, 0xcd, 0x40, 0xac, 0xe0, 0x49, 0x60, 0x83, 0x80, 0xdd, 0x80, 0x10, 0xe0, 0xe5, 0x80, 0xb4, 0xe0,
    0xed, 0x00, 0x83, 0x01, 0x02, 0x03, 0x04, 0x81, 0x05, 0x06, 0x07, 0x00, 0x88, 0x08, 0x04, 0x81, 0x09, 0x0a, 0x0b, 0x0c, 0x04, 0x81, 0x02, 0x00, 0x86, 0x0d, 0x04, 0x0e, 0x0f,
    0x00, 0x83, 0x10, 0x0d, 0x04, 0x0b, 0x00, 0x84, 0x11, 0x04, 0x12, 0x00, 0x87, 0x13, 0x04, 0x14, 0x00, 0x82, 0x15, 0x04, 0x16, 0x00, 0x89, 0x14, 0x04, 0x17, 0x00, 0x81, 0x18,
    0x04, 0x19, 0x00, 0x8a, 0x1a, 0x04, 0x05, 0x0b, 0x05, 0x1b, 0x00, 0x8b, 0x1c, 0x04, 0x81, 0x16, 0x04, 0x1d, 0x00, 0x8b, 0x1e, 0x04, 0x81, 0x1f, 0x04, 0x20, 0x00, 0x8b, 0x21,
    0x04, 0x1d, 0x00, 0x03, 0x1b, 0x00, 0x8b, 0x0e, 0x04, 0x22, 0x00, 0x23, 0x04, 0x07, 0x00, 0x8a, 0x0d, 0x24, 0x00, 0x81, 0x25, 0x04, 0x20, 0x00, 0x8f, 0x18, 0x04, 0x02, 0x00,
    0x8f, 0x1c, 0x04, 0x0b, 0x19, 0x00, 0x8e, 0x02, 0x04, 0x81, 0x26, 0x27, 0x08, 0x00, 0x8c, 0x1f, 0x14, 0x09, 0x04, 0x28, 0x00, 0x88,
};
const unsigned char bmp_bpm_2[] = {
    /* 数组大小：182 颜色数：16 图片：6008_bpm_2.bmp*/
    0x0f, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x00, 0x00, 0xcb, 0x5a, 0x96, 0xb5, 0x5d, 0xef, 0xdf, 0xff, 0x04, 0x21, 0x59, 0xce, 0x39, 0xfe, 0x96, 0xf5, 0xff, 0xff, 0xaf, 0xe3, 0x23, 0xd8, 0x02,
    0xe0, 0xa8, 0xd9, 0x1c, 0xe7, 0x8f, 0xe3, 0x00, 0x83, 0x01, 0x02, 0x03, 0x04, 0x03, 0x02, 0x01, 0x00, 0x85, 0x05, 0x06, 0x04, 0x81, 0x07, 0x08, 0x07, 0x04, 0x81, 0x06, 0x05, 0x00, 0x82,
    0x05, 0x03, 0x09, 0x0a, 0x0b, 0x0c, 0x82, 0x0b, 0x0a, 0x09, 0x03, 0x05, 0x00, 0x81, 0x06, 0x04, 0x0d, 0x0c, 0x86, 0x0d, 0x04, 0x06, 0x00, 0x01, 0x04, 0x0a, 0x0c, 0x88, 0x0a, 0x04, 0x01,
    0x02, 0x04, 0x0b, 0x0c, 0x88, 0x0b, 0x04, 0x02, 0x03, 0x07, 0x0c, 0x8a, 0x07, 0x0e, 0x04, 0x08, 0x0c, 0x8a, 0x08, 0x04, 0x03, 0x07, 0x0c, 0x8a, 0x07, 0x0e, 0x02, 0x04, 0x0b, 0x0c, 0x88,
    0x0b, 0x04, 0x02, 0x01, 0x04, 0x0f, 0x0c, 0x88, 0x0a, 0x04, 0x01, 0x00, 0x06, 0x04, 0x0d, 0x0c, 0x86, 0x0d, 0x04, 0x06, 0x00, 0x81, 0x05, 0x03, 0x09, 0x0a, 0x0b, 0x0c, 0x82, 0x0b, 0x0a,
    0x09, 0x03, 0x05, 0x00, 0x82, 0x05, 0x06, 0x04, 0x81, 0x07, 0x08, 0x07, 0x04, 0x81, 0x06, 0x05, 0x00, 0x85, 0x01, 0x02, 0x0e, 0x04, 0x0e, 0x02, 0x01, 0x00, 0x83,
};
const unsigned char bmp_bpm_1[] = {
    /* 数组大小：182 颜色数：14 图片：6007_bpm_1.bmp*/
    0x0f, 0x00, 0x0f, 0x00, 0x0e, 0x00, 0x00, 0x00, 0xcb, 0x5a, 0x96, 0xb5, 0x5d, 0xef, 0xdf, 0xff, 0x04, 0x21, 0x59, 0xce, 0x1c, 0xe7, 0x4d, 0x6b, 0xc3, 0x18, 0xd7, 0xbd, 0x41, 0x08, 0xdb,
    0xde, 0x0c, 0x63, 0x00, 0x83, 0x01, 0x02, 0x03, 0x04, 0x03, 0x02, 0x01, 0x00, 0x85, 0x05, 0x06, 0x04, 0x86, 0x06, 0x05, 0x00, 0x82, 0x05, 0x03, 0x04, 0x07, 0x08, 0x09, 0x00, 0x09, 0x08,
    0x07, 0x04, 0x03, 0x05, 0x00, 0x81, 0x06, 0x04, 0x0a, 0x0b, 0x00, 0x84, 0x0b, 0x0a, 0x04, 0x06, 0x00, 0x01, 0x04, 0x0c, 0x0b, 0x00, 0x86, 0x0b, 0x0c, 0x04, 0x01, 0x02, 0x04, 0x0d, 0x00,
    0x88, 0x08, 0x04, 0x02, 0x03, 0x04, 0x09, 0x00, 0x88, 0x09, 0x04, 0x07, 0x04, 0x81, 0x00, 0x8a, 0x04, 0x81, 0x03, 0x04, 0x09, 0x00, 0x88, 0x09, 0x04, 0x07, 0x02, 0x04, 0x0d, 0x00, 0x88,
    0x08, 0x04, 0x02, 0x01, 0x04, 0x0c, 0x0b, 0x00, 0x86, 0x0b, 0x0c, 0x04, 0x01, 0x00, 0x06, 0x04, 0x02, 0x0b, 0x00, 0x84, 0x0b, 0x02, 0x04, 0x06, 0x00, 0x81, 0x05, 0x03, 0x04, 0x07, 0x0d,
    0x09, 0x00, 0x09, 0x0d, 0x07, 0x04, 0x03, 0x05, 0x00, 0x82, 0x05, 0x06, 0x04, 0x86, 0x06, 0x05, 0x00, 0x85, 0x01, 0x02, 0x07, 0x04, 0x07, 0x02, 0x01, 0x00, 0x83,
};

void view_metronome_base_display(void)
{
#ifdef CUSTOM_UI
    int const temp_index_start = 50;
    int custom_ui_index = 0;

    for (int i = 0; i < METRONOME_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_metronome_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if METRONOME_GIF_COUNT > 0
    disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_metronome_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif
    int x = 60;
    int y = 174;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, x, y, FILE_IMG_6002_main_metronome_text);
    }
    //  bool ret = disp_show_localize(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_string_key_metronome_text, temp_index_start + custom_ui_index);
    // if (!ret)
    // {
    //     _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, 60, 174, FILE_IMG_6002_main_metronome_text);
    // }
    ++custom_ui_index;
#else
    int x = 49;
    int y = 40;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_6001_main_metronome);
    x = 60;
    y = 174;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_6002_main_metronome_text);
    }
    show_button_up();
    show_button_right();
    show_button_down();
#endif
}

APP_TIMER_DEF(metronome_timer_id); //佩戴检测
APP_TIMER_DEF(metronome_timer_second_id);
typedef struct __metronome_instance_t
{
    bool is_init;
    int seconds;
    int bpm;
    bool is_shake;
    bool is_run;
    uint64_t count;
} metronome_instance_t;
static metronome_instance_t metronome_instance;

static void metronome_timer_second_handle(void *p_context)
{
    metronome_instance.seconds++;
}
static void metronome_timer_handle(void *p_context)
{
    metronome_instance.is_shake = !metronome_instance.is_shake;
    motor_start(100);
    metronome_instance.count++;
}
bool is_bottom_reset()
{
    if (metronome_instance.seconds != 0 && !metronome_instance.is_run)
    {
        //!< 暂停切时间不为0，进行reset
        return true;
    }
    else
    {
        return false;
    }
}
void view_metronome_state_display(void)
{
    int x = 159;
    int y = 138;
    disp_draw_user_image(x, y, bmp_6009_unit_bpm);
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = metronome_instance.seconds;
        disp_ctrl.com_data.i_temp[1] = metronome_instance.is_run;
        disp_ctrl.com_data.i_temp[2] = metronome_instance.bpm;
        disp_ctrl.com_data.i_temp[3] = metronome_instance.is_shake;
        if (disp_ctrl.com_data.i_temp[3] == disp_ctrl.com_data.i_temp[6] && disp_ctrl.com_data.i_temp[5] != metronome_instance.count)
        {
            //!< count不一样了，但是is_shake还是跟之前的一样，那么我也要假装不一样
            disp_ctrl.com_data.i_temp[3] = !disp_ctrl.com_data.i_temp[3];
        }
        disp_ctrl.com_data.i_temp[5] = metronome_instance.count;
        disp_ctrl.com_data.i_temp[6] = disp_ctrl.com_data.i_temp[3];
        disp_ctrl.auto_refresh_time = 0;
        int rtc_time = rtc_get_utc();
        if (disp_ctrl.com_data.i_temp[4] != rtc_time)
        {
            disp_ctrl.area_update_flag = 0;
        }
        else
        {
            display_set_update_area(49, 119, 15, 15);
        }
        disp_ctrl.com_data.i_temp[4] = rtc_time;
    }
    int hour = disp_ctrl.com_data.i_temp[0] / 60 / 60 % 99;
    int min = disp_ctrl.com_data.i_temp[0] / 60 % 60;
    int second = disp_ctrl.com_data.i_temp[0] % 60;
    x = 63;
    y = 29;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour / 10]);
    x = 78;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour % 10]);
    x = 105;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[min / 10]);
    x = 121;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[min % 10]);
    x = 148;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second / 10]);
    x = 164;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second % 10]);
    x = 96;
    disp_draw_user_image(x, y, num_N312_second_6x32);
    x = 139;
    disp_draw_user_image(x, y, num_N312_second_6x32);

    x = 25;
    y = 194;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_6006_setfrequency);

    x += 36;
    y += 7;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_6006_setfrequency_36_7_38_22);
    }

    if (is_bottom_reset())
    {
        x = 198;
        y = 51;
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_6110_reset);
    }
    x = 49;
    y = 119;
    if (disp_ctrl.com_data.i_temp[3])
    {
        disp_draw_user_image(x, y, bmp_bpm_2);
    }
    else
    {
        disp_draw_user_image(x, y, bmp_bpm_1);
    }

    x = 219;
    y = 113;
    if (disp_ctrl.com_data.i_temp[1])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_6003_start);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_6004_timeout);
    }

    int hundreds = disp_ctrl.com_data.i_temp[2] / 100;
    int tens = disp_ctrl.com_data.i_temp[2] / 10 % 10;
    int units = disp_ctrl.com_data.i_temp[2] % 10;

    x = 71;
    y = 94;
    if (hundreds != 0)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, hundreds + FILE_IMG_N500_0_25x59);
    }
    x = 98;
    if (tens != 0 || hundreds != 0)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, tens + FILE_IMG_N500_0_25x59);
    }
    x = 125;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, units + FILE_IMG_N500_0_25x59);
}

void view_metronome_option_display(void)
{
    int x = 22;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_6010_setfrequency_back);
    x += 38;
    y += 22;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_6006_setfrequency_36_7_38_22);
    }

    x = 57;
    y = 85;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_6011_select);

    x = 153;
    y = 116;
    disp_draw_user_image(x, y, bmp_6009_unit_bpm);

    show_button_right();

    int hundreds = disp_ctrl.com_data.i_temp[0] / 100;
    int tens = disp_ctrl.com_data.i_temp[0] / 10 % 10;
    int units = disp_ctrl.com_data.i_temp[0] % 10;
    y = 99;
    if (hundreds != 0)
    {
        x = 75;
        disp_draw_user_image(x, y, num_N400_dig_19x42_TAB[hundreds]);
    }
    if (tens != 0 || hundreds != 0)
    {
        x = 98;
        disp_draw_user_image(x, y, num_N400_dig_19x42_TAB[tens]);
    }
    x = 121;
    disp_draw_user_image(x, y, num_N400_dig_19x42_TAB[units]);
    if (disp_ctrl.com_data.i_temp[0] > 10)
    {
        show_button_up();
    }
    if (disp_ctrl.com_data.i_temp[0] < 240)
    {
        show_button_down();
        hundreds = (disp_ctrl.com_data.i_temp[0] + 1) / 100;
        tens = (disp_ctrl.com_data.i_temp[0] + 1) / 10 % 10;
        units = (disp_ctrl.com_data.i_temp[0] + 1) % 10;

        y = 165;
        if (hundreds != 0)
        {
            x = 75;
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, hundreds + FILE_IMG_6100_bpm_unselected_0_19x42);
        }
        if (tens != 0 || hundreds != 0)
        {
            x = 98;
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, tens + FILE_IMG_6100_bpm_unselected_0_19x42);
        }
        x = 121;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, units + FILE_IMG_6100_bpm_unselected_0_19x42);
    }
}

int32_t view_metronome_display(int32_t view)
{
    Expects(view >= view_metronome);
    Expects(view < view_metronome + VIEW_LEVEL_0_SUB_COUNT);
    switch (view)
    {
    case view_metronome_base:
        view_metronome_base_display();
        break;
    case view_metronome_state:
        view_metronome_state_display();
        break;
    case view_metronome_option:
        view_metronome_option_display();
        break;
    default:
        break;
    }

    return 0;
}
view_event_result_t view_metronome_event(int32_t view, event_param_t event)
{
    Expects(view >= view_metronome);
    Expects(view < view_metronome + VIEW_LEVEL_0_SUB_COUNT);
    switch (view)
    {
    case view_metronome_base:
        switch (event.event)
        {
        case event_view_entry:
        {
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Metronome, buff, buff_len);
            return view_event_result_handled;
        }
        }
        break;
    case view_metronome_state:
    {
        switch (event.event)
        {
        case BUTTON_EVENT_TP_TAP:
            LOG("BUTTON_EVENT_TP_TAP %d", event.y);
            if (event.y > 180)
            {
                display_show(view_metronome_option, DISP_MODE_NORMAL);
            }
            else
            {
                if (event.y < 80 && is_bottom_reset())
                {
                    metronome_instance.seconds = 0;
                    metronome_instance.count = 0;
                    display_set_update_request(view_metronome_state, 0);
                }
                else
                {
                    LOG("BUTTON_EVENT_TP_TAP toggle");
                    metronome_instance.is_run = !metronome_instance.is_run;
                    if (metronome_instance.is_run)
                    {
                        app_timer_start(metronome_timer_id, APP_TIMER_TICKS(1000 * 60 / metronome_instance.bpm, APP_TIMER_PRESCALER), 0);
                        app_timer_start(metronome_timer_second_id, APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER), 0);
                    }
                    else
                    {
                        app_timer_stop(metronome_timer_id);
                        app_timer_stop(metronome_timer_second_id);
                    }
                    display_set_update_request(view_metronome_state, 0);
                }
            }
            return view_event_result_handled;
        case event_view_entry:
        {
            if (!metronome_instance.is_init)
            {
                metronome_instance.is_init = true;
                uint32_t err_code = app_timer_create(&metronome_timer_id, APP_TIMER_MODE_REPEATED, &metronome_timer_handle);
                Expects(err_code == NRF_SUCCESS);
                err_code = app_timer_create(&metronome_timer_second_id, APP_TIMER_MODE_REPEATED, &metronome_timer_second_handle);
                Expects(err_code == NRF_SUCCESS);
            }
            if (event.previous_view != view_metronome_option)
            {
                metronome_instance.seconds = 0;
                metronome_instance.count = 0;
            }
            metronome_instance.bpm = get_metronome_hz();
            metronome_instance.is_shake = false;
            metronome_instance.is_run = false;
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Set_frequency, buff, buff_len);
            return view_event_result_handled;
        }
        case event_view_exit:
            app_timer_stop(metronome_timer_id);
            app_timer_stop(metronome_timer_second_id);

            return view_event_result_handled;
        case event_move_previous_view:
            if (is_bottom_reset())
            {
                metronome_instance.seconds = 0;
                metronome_instance.count = 0;
                display_set_update_request(view_metronome_state, 0);
            }
            return view_event_result_handled;
        case event_move_next_view:
            display_show(view_metronome_option, DISP_MODE_NORMAL);
            return view_event_result_handled;
        case event_move_level_down:
            metronome_instance.is_run = !metronome_instance.is_run;
            if (metronome_instance.is_run)
            {
                app_timer_start(metronome_timer_id, APP_TIMER_TICKS(1000 * 60 / metronome_instance.bpm, APP_TIMER_PRESCALER), 0);
                app_timer_start(metronome_timer_second_id, APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER), 0);
            }
            else
            {
                app_timer_stop(metronome_timer_id);
                app_timer_stop(metronome_timer_second_id);
            }
            display_set_update_request(view_metronome_state, 0);
            return view_event_result_handled;
        }
        break;
    }
    case view_metronome_option:
    {
        switch (event.event)
        {
        case BUTTON_EVENT_TP_TAP:
            if (event.y > 160)
            { //!< 设置bpm
                if (disp_ctrl.com_data.i_temp[0] < 240)
                {
                    disp_ctrl.com_data.i_temp[0]++;
                    display_set_update_request(view_metronome_option, 0);
                }
            }
            else if (event.y < 80)
            {
                //!< 设置bpm
                if (disp_ctrl.com_data.i_temp[0] > 10)
                {
                    disp_ctrl.com_data.i_temp[0]--;
                    display_set_update_request(view_metronome_option, 0);
                }
            }
            else
            { //!< 设置ok
                set_metronome_hz(disp_ctrl.com_data.i_temp[0]);
                display_show(view_metronome_state, DISP_MODE_NORMAL);
            }
            return view_event_result_handled;
        case event_view_entry:
        {
            disp_ctrl.com_data.i_temp[0] = get_metronome_hz();
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Set_frequency, buff, buff_len);
            return view_event_result_handled;
        }
        case BUTTON_EVENT_TP_MOVE_LEFT:
            return view_event_result_handled;
        case BUTTON_EVENT_LONG_PRESS:
        case BUTTON_EVENT_LONG_PRESS_KEEP:
            switch (event.source)
            {
#if defined(BUTTON_TOP)
            case event_source_button_top:
                LOG("metronome +10");
                if (disp_ctrl.com_data.i_temp[0] > 20)
                {
                    disp_ctrl.com_data.i_temp[0] -= 10;
                    display_set_update_request(view_metronome_option, 0);
                }
                else if (disp_ctrl.com_data.i_temp[0] > 10)
                {
                    disp_ctrl.com_data.i_temp[0] = 10;
                    display_set_update_request(view_metronome_option, 0);
                }
                break;
#endif
#if defined(BUTTON_BOTTOM)
            case event_source_button_bottom:
                LOG("metronome -10");
                if (disp_ctrl.com_data.i_temp[0] < 230)
                {
                    disp_ctrl.com_data.i_temp[0] += 10;
                    display_set_update_request(view_metronome_option, 0);
                }
                else if (disp_ctrl.com_data.i_temp[0] < 240)
                {
                    disp_ctrl.com_data.i_temp[0] = 240;
                    display_set_update_request(view_metronome_option, 0);
                }
                break;
#endif
            }
            return view_event_result_handled;
        case event_move_previous_view:
            LOG("metronome +1");
            //!< 设置bpm
            if (disp_ctrl.com_data.i_temp[0] > 10)
            {
                disp_ctrl.com_data.i_temp[0]--;
                display_set_update_request(view_metronome_option, 0);
            }
            return view_event_result_handled;
        case event_move_next_view:
            LOG("metronome -1");
            //!< 设置bpm
            if (disp_ctrl.com_data.i_temp[0] < 240)
            {
                disp_ctrl.com_data.i_temp[0]++;
                display_set_update_request(view_metronome_option, 0);
            }
            return view_event_result_handled;
        case event_move_level_down:
            //!< 设置ok
            set_metronome_hz(disp_ctrl.com_data.i_temp[0]);
            display_show(view_metronome_state, DISP_MODE_NORMAL);
            return view_event_result_handled;
        }
        break;
    }
    }
    return view_event_result_not_handled;
}
